﻿// ***********************************************************************
// Project			: Micua
// Assembly         : Micua.Core
// Author           : iceStone
// Created          : 2013-11-23 23:13
//
// Last Modified By : iceStone
// Last Modified On : 2013-11-23 23:13
// ***********************************************************************
// <copyright file="MenuNav.cs" company="Wedn.Net">
//     Copyright (c) Wedn.Net. All rights reserved.
// </copyright>
// <summary>缺少注释</summary>
// ***********************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using Micua.Core.Caching;
using Micua.Model;
using Micua.Model.Enum;

namespace Micua.UI.Model
{
    /// <summary>
    /// 菜单导航
    /// </summary>
    /// <remarks>
    ///  2013-11-23 23:13 Created By iceStone
    /// </remarks>
    public class MenuNav
    {
        /// <summary>
        /// 子UL 类名
        /// </summary>
        private readonly string _subUlClass;
        /// <summary>
        /// 单项类名
        /// </summary>
        private readonly string _itemClass;
        /// <summary>
        /// 菜单HTML构造者
        /// </summary>
        private readonly StringBuilder _menuHtmlBuilder;
        /// <summary>
        /// 菜单项集合
        /// </summary>
        private readonly List<Link> _linkList;
        /// <summary>
        /// 当前请求URL(去除页码的)
        /// </summary>
        private readonly string _currentUrl;
        /// <summary>
        /// 当前菜单
        /// </summary>
        private readonly Link _currentNav;
        /// <summary>
        /// 缓存的导航菜单HTML
        /// </summary>
        private MvcHtmlString _cacheMenuNavHtml;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <remarks>
        ///  2013-11-23 23:13 Created By iceStone
        /// </remarks>
        /// <param name="rawUrl">当前请求URL(不包括QueryString)</param>
        /// <param name="ulId">总UL id</param>
        /// <param name="ulClass">总UL class</param>
        /// <param name="subUlClass">子UL class</param>
        /// <param name="itemClass">单项class</param>
        public MenuNav(string rawUrl, string ulId, string ulClass, string subUlClass, string itemClass)
        {
            _currentUrl = Regex.Replace(rawUrl, @"(page)?/\d/?", string.Empty);
            _cacheMenuNavHtml = CacheHelper.Get<MvcHtmlString>(_currentUrl);
            if (_cacheMenuNavHtml != null) return;//如果有缓存
            _subUlClass = subUlClass;
            _itemClass = itemClass; 
            _linkList = ServiceFactory.ServiceFactory.Instance.LinkService.Query(l => l.Type == (byte)LinkType.Navigation && l.Visible).OrderBy(l => l.Order).ToList();
            _currentNav = _linkList.FirstOrDefault(n => String.Equals(GetHttpUrl(n.Url), GetHttpUrl(_currentUrl), StringComparison.CurrentCultureIgnoreCase));
            _menuHtmlBuilder = new StringBuilder(string.Format("<ul id=\"{0}\" class=\"{1}\">\r\n", ulId, ulClass));
        }
        /// <summary>
        /// 渲染菜单导航
        /// </summary>
        /// <remarks>
        ///  2013-11-23 23:13 Created By iceStone
        /// </remarks>
        /// <returns>菜单导航Html</returns>
        public MvcHtmlString Render()
        {
            if (_cacheMenuNavHtml != null) return _cacheMenuNavHtml;//如果有缓存,返回缓存
            foreach (var item in _linkList.Where(l => l.ParentId == null))
            {
                _menuHtmlBuilder.AppendFormat(
                    "\t<li id=\"menu_item_{0}\" class=\"{1} menu-item-{0}\">\r\n\t\t<a href=\"{2}\" title=\"{3}\" target=\"{4}\" rel=\"{5}\">{6}</a>\r\n",
                    item.Id,
                    _currentNav != null && _currentNav.Id == item.Id
                    ? string.Format("current-{0} {0}", _itemClass)
                    : _itemClass,
                    item.Url,
                    item.Title,
                    item.Target,
                    item.Rel,
                    item.Text);
                RenderSubMenuNav(item.Id);
                _menuHtmlBuilder.Append("\t</li>\r\n");
            }
            _menuHtmlBuilder.Append("\t</ul>");
            _cacheMenuNavHtml = new MvcHtmlString(_menuHtmlBuilder.ToString());
            CacheHelper.Set(_currentUrl, _cacheMenuNavHtml,TimeSpan.FromDays(1));
            return _cacheMenuNavHtml;

        }
        /// <summary>
        /// 递归子菜单
        /// </summary>
        /// <remarks>
        ///  2013-11-23 23:13 Created By iceStone
        /// </remarks>
        /// <param name="parentId">The parent identifier.</param>
        private void RenderSubMenuNav(int parentId)
        {
            var childs = _linkList.Where(l => l.ParentId == parentId).ToList();
            if (!childs.Any()) return;
            _menuHtmlBuilder.AppendFormat("\t\t<ul class=\"{0}\">\r\n", _subUlClass);
            foreach (var item in childs)
            {
                _menuHtmlBuilder.AppendFormat(
                    "\t\t\t<li id=\"menu_item_{0}\" class=\"{1} menu-item-{0}\">\r\n\t\t\t\t<a href=\"{2}\" title=\"{3}\" target=\"{4}\" rel=\"{5}\">{6}</a>\r\n",
                    item.Id,
                    _currentNav != null && _currentNav.Id == item.Id
                    ? string.Format("current-{0} {0}", _itemClass)
                    : _itemClass,
                    item.Url,
                    item.Title,
                    item.Target,
                    item.Rel,
                    item.Text);
                RenderSubMenuNav(item.Id);
                _menuHtmlBuilder.Append("\t\t\t</li>\r\n");
            }
            _menuHtmlBuilder.Append("\t\t</ul>");
        }
        /// <summary>
        /// 获取URL
        /// </summary>
        /// <remarks>
        ///  2013-11-23 23:13 Created By iceStone
        /// </remarks>
        /// <param name="url">The URL.</param>
        /// <returns>System.String.</returns>
        private string GetHttpUrl(string url)
        {
            //if (url.IndexOf("://", StringComparison.Ordinal) == -1)
            //    url = Common.Config.SiteUrl + url.TrimStart('/');
            return url;
        }
    }
}